package com.opennews.openplatform.config;

import com.opennews.openplatform.myspringbootcore.security.RequestMapManager;
import com.opennews.openplatform.security.SecurityActiveTokenManager;
import com.opennews.openplatform.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class DataInitializer implements CommandLineRunner {
    private final AppReleaseLogService appReleaseLogService;
    private final BankService bankService;
    private final IncomeExpenseDetailBaseTypeService incomeExpenseDetailBaseTypeService;
    private final RegionService regionService;
    private final RequestMapService requestMapService;
    private final RequestMapManager requestMapManager;
    private final RoleService roleService;
    private final SecurityActiveTokenManager securityActiveTokenManager;

    @Override
    public void run(String... args) {
        // For Initializing data if needed.
        appReleaseLogService.init();
        bankService.init();
        incomeExpenseDetailBaseTypeService.init();
        regionService.init();
        requestMapService.init();
        roleService.init();

        // To make the setting work we have to clear the cache.
        // Just in case any error causes requestMap failed. CLears the cache once system startup.
        // For example, the Requestmap.httpMethod value must be uppercase. Lowercase value would cause clearCachedRequestmaps failed.
        // Once clearCachedRequestmaps failed, any access will be blocked.
        // BE CAUTION!!!!
        //
        // IMPORTANT!!!
        // If you see the error of "No enum constant org.springframework.http.HttpMethod." when you are trying to run project, go and
        // check the Requestmap.httpMethod values in database table.
        // This field only accepts the string value of http methods. Any empty string of other string values will cause this error.
        requestMapManager.reloadCachedRequestMaps();

        securityActiveTokenManager.reloadCachedUserActiveTokenRecords();
    }
}
